var prefix = "/jzweb/bookOrder";
$(function(){
	//学校查询
	$('#school').selectPage({
		showField: 'name',
		keyField: 'name',
		data: prefix+'/selectSchool',
		//向服务端提交的参数中，增加已选中的
		//设置返回-1，意为通知服务端返回空列表，初始化时使用
		params: function(){
			var pv = $('#school').val();
			return pv;
		},
		eAjaxSuccess : function(d){
			var result;
			if(d) result = d.data;
			else result = undefined;
			return result;
		}
	});

});


$.get(
	'/jzweb/examinationCost/userProvinceSel',
	{ type: 0 },
	function (data){
		let str = '';
		if( data.code == 0 ){
			str = `<option value="">--请选择省份--</option>`;
			data.data.forEach(function (v){
				str += `<option value="${v.name}">${v.name}</option>`
			})
		}
		$("#orPro").html(str)
	}
)

$.get(
	'/jzweb/examinationCost/userProvinceSel',
	{ type: 1 },
	function (data){
		let str = '';
		if( data.code == 0 ){
			str = `<option value="">--请选择城市--</option>`;
			data.data.forEach(function (v){
				str += `<option value="${v.name}">${v.name}</option>`
			})
		}
		$("#orCity").html(str)
	}
)

$(function(){
	//学校查询
	$('#examFeeIf').selectPage({

		showField: 'name',
		keyField: 'id',
		multiple : true,
		data: prefix+'/selectCost2?year='+'2022',
		//向服务端提交的参数中，增加已选中的
		//设置返回-1，意为通知服务端返回空列表，初始化时使用
		params: function(){
			var pv = $('#examFeeIf').val();
			return pv;
		},
		eAjaxSuccess : function(d){
			var result;
			if(d) result = d.data;
			else result = undefined;
			return result;
		}
	});

});





$().ready(function() {
	validateRule();

});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function save() {
	// document.getElementById("submit").disabled=true;
	$.ajax({
		cache : true,
		type : "post",
		url : "/jzweb/bookOrder/saveYear",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}


$("#orderNumber").blur(function (){
	sumPrice();
})

$("#discount").blur(function (){
	sumPrice();
})
$("#orderPrimary").blur(function (){
	sumPrice();
})
$("#discountPrimary").blur(function (){
	sumPrice();
})
$("#orderIntermediate").blur(function (){
	sumPrice();
})
$("#discountIntermediate").blur(function (){
	sumPrice();
})
$("#orderSenior").blur(function (){
	sumPrice();
})
$("#discountSenior").blur(function (){
	sumPrice();
})

function sumPrice(){

	if( $("#bookId").val() && $("#orderNumber").val() && $("#discount").val() && $("#orderPrimary").val() && $("#discountPrimary").val() && $("#orderIntermediate").val() && $("#discountIntermediate").val() && $("#orderSenior").val() && $("#discountSenior").val() ){

		$.ajax({
			cache : true,
			type : "get",
			url : "/jzweb/bookOrder/bookOrderSum",
			data : {
				bookId: $("#bookId").val(),
				orderNumber:$("#orderNumber").val(),
				discount: $("#discount").val(),
				orderPrimary: $("#orderPrimary").val(),
				discountPrimary: $("#discountPrimary").val(),
				orderIntermediate: $("#orderIntermediate").val(),
				discountIntermediate: $("#discountIntermediate").val(),
				orderSenior: $("#orderSenior").val(),
				discountSenior: $("#discountSenior").val()
			},// 你的formid
			async : false,
			error : function(request) {
				parent.layer.alert("Connection error");
			},
			success : function(data) {

				if (data.code == 0) {
					$("#bookOrderSum").val(data.data.money);
				} else {
					layer.alert(data.msg)
				}

			}
		});

	}

}

layui.use('upload', function () {
	var upload = layui.upload;
	//执行实例
	upload.render({
		elem: '#btnUploadOrder', //绑定元素
		url: '/common/sysFile/upload', //上传接口
		accept: 'file',
		done: function (r) {
			$("#exlUrl").val(r.data);
			$("#test").html(subFileName(r.data));
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			bookId : {
				required : true
			},
			orderNumber : {
				required : true
			},
			orderPrimary : {
				required : true
			},
			orderSenior : {
				required : true
			},
			orderIntermediate : {
				required : true
			},
			discount : {
				required : true
			},
			province : {
				required : true
			},
			address : {
				required : true
			},
			school : {
				required : true
			},
			year : {
				required : true
			},
			discountPrimary : {
				required : true
			},
			discountIntermediate : {
				required : true
			},
			discountSenior : {
				required : true
			},

			addressee : {
				required : true
			},
			orderTypes : {
				required : true
			},
			orProd : {
				required : true
			},
			orCityd : {
				required : true
			}

		},
		messages : {
			bookId : {
				required : icon + "请选择对应书籍种类"
			},
			orderNumber : {
				required : icon + "请输入理论订单数量"
			},
			orderPrimary : {
				required : icon + "请输入理论初级数量"
			},
			orderIntermediate : {
				required : icon + "请输入理论中级数量"
			},
			orderSenior : {
				required : icon + "请输入理论高级数量"
			},
			discount : {
				required : icon + "请输入折扣价"
			},
			province : {
				required : icon + "请输入省份"
			},
			address : {
				required : icon + "请输入详细地址"
			},
			school : {
				required : icon + "请输入单位名称或学校名称"
			},
			year : {
				required : icon + "年份必须填写"
			},
			discountPrimary : {
				required : icon + "初级折扣必填"
			},
			discountIntermediate : {
				required : icon + "中级折扣必填"
			},
			discountSenior : {
				required : icon + "高级折扣必填"
			},
			addressee : {
				required : icon + "请输入收件人"
			},
			orderTypes : {
				required : icon + "请选择订单类型"
			},orProd : {
				required : icon + "请输入收件人"
			}
			,orCityd : {
				required : icon + "请输入收件人"
			}

		}
	})
}

$(function () {
	$("#orderTypes").change(function () {
		var type = $(this).val();
		if (type != '') {

			if (type == 0) {  //为视频格式
				$("#schoolDiv").show();
				$("#schoolDiv1").hide();
			} else  {  //为课件（ppt、pptx）格式
				$("#schoolDiv").hide();
				$("#schoolDiv1").show();
			}
		}

	});


});

$(function(){
	//学校查询
	$('#refundUser').selectPage({
		showField: 'name',
		keyField: 'id',
		data: prefix+'/selectUserBy1and2',
		//向服务端提交的参数中，增加已选中的
		//设置返回-1，意为通知服务端返回空列表，初始化时使用
		params: function(){
			var pv = $('#refundUser').val();
			return pv;
		},
		eAjaxSuccess : function(d){
			var result;
			if(d) result = d.data;
			else result = undefined;
			return result;
		}
	});
});

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			refundUser : {
				required : true
			}
		}
	})
}

layui.use('laydate', function(){

	var laydate = layui.laydate;

	laydate.render({
		elem: '#orderCreatetime'
		,type: 'datetime'
	});

})